package com.zkk.service.impl;

import com.zkk.dao.JdbcCardDao;
import com.zkk.dao.impl.JdbcCardDaoImpl;
import com.zkk.service.JdbcCardService;
import com.zkk.utils.JdbcUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

public class JdbcCardServiceImpl implements JdbcCardService {
    JdbcCardDao jdbcCardDao = new JdbcCardDaoImpl();
    @Override
    public int onInputMoney(String id_card, String card_no, BigDecimal money) {
        Connection connection = JdbcUtils.getConnection();
        try {
            connection.setAutoCommit(false);
            //开始转账
            System.out.println("开始转账");
            int on = jdbcCardDao.transferMoney(money, id_card, connection);
            //收到转账
            int in = jdbcCardDao.inTransferMoney(money, card_no, connection);
            System.out.println("转账完成");
            connection.commit();
        } catch (Exception e) {
            try {
                connection.rollback();
                System.out.println("转账失败");
            } catch (SQLException ex) {

            }
        }
        return 1;
    }
}
